package com.sywb.chuangyebao.view;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.utils.GlideUtils;

import org.bining.footstone.bean.ImagePath;
import org.bining.footstone.presenter.BasePresenter;
import org.bining.footstone.utils.ToastUtils;

public abstract class ActionbarActivity<T extends BasePresenter> extends BaseStatisticsActivity<T> {

    protected FrameLayout content;

    protected RelativeLayout actionbar;
    private ImageView actionbarBack;
    private ImageView actionbarTips;
    private TextView actionbarTitle;
    private LinearLayout actionbarMenu;
    private View actionbarBottomLine;

    private RelativeLayout loading;
    private ImageView loadingImage;
    private ProgressBar loadingProgress;
//    private LottieAnimationView loadingLottie;

    @Override
    public void setContentView(View view) {
        View rootView = getLayoutInflater().inflate(R.layout.activity_actionbar, null);
        FrameLayout content = rootView.findViewById(R.id.content);
        content.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initTitle();
        super.setContentView(rootView);
    }

    protected void initTitle() {
    }

    @Override
    public void initView(Bundle bundle) {
        actionbar = getView(R.id.actionbar);
        actionbarBack = getView(R.id.actionbar_back);
        actionbarTips = getView(R.id.actionbar_tips);
        actionbarTitle = getView(R.id.actionbar_title);
        actionbarMenu = getView(R.id.actionbar_menu);
        actionbarBottomLine = getView(R.id.actionbar_bottom_line);
        content = getView(R.id.content);
        loading = getView(R.id.loading);
        loadingImage = getView(R.id.loading_image);
        loadingProgress = getView(R.id.loading_progress);
//        loadingLottie = getView(R.id.loading_lottie);

        actionbarBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackClick();
            }
        });
    }

    /**
     * 批量设置控件的显示状态
     *
     * @param isVisible 是否显示
     * @param views     控件集
     */
    public void setVisibility(boolean isVisible, View... views) {
        if (isVisible) {
            setVisibility(View.VISIBLE, views);
        } else {
            setVisibility(View.GONE, views);
        }
    }

    /**
     * 批量设置控件的显示状态
     *
     * @param visible 显示状态
     * @param views   控件集
     */
    public void setVisibility(int visible, View... views) {
        for (View view : views) {
            if (view.getVisibility() == visible) {
                continue;
            }

            view.setVisibility(visible);
        }
    }

    /**
     * 设置是否显示加载进度
     *
     * @param isVisible 是否显示
     */
    public void hasProgress(boolean isVisible) {
        hasProgress(1, isVisible);
    }

    /**
     * 设置是否显示加载进度
     *
     * @param type      进度类型 1:gif 2:pregress 3:lottie
     * @param isVisible 是否显示
     */
    public void hasProgress(int type, boolean isVisible) {
        switch (type) {
            case 1:
                if (isVisible) {
                    if (loadingImage.getVisibility() == View.VISIBLE) {
                        return;
                    }
                    loadingImage.setVisibility(View.VISIBLE);
                    Glide.with(this).load(R.drawable.common_loading).into(loadingImage);
                } else {
                    if (loadingImage.getVisibility() == View.GONE) {
                        return;
                    }
                    loadingImage.setVisibility(View.GONE);
                    loadingImage.setImageResource(R.drawable.common_loading);
                }
                break;
            case 2:
                if (isVisible && loadingProgress.getVisibility() == View.VISIBLE) {
                    return;
                }
                setVisibility(isVisible, loadingProgress);
                break;
            case 3:
//                if (isVisible) {
//                    if (loadingLottie.getVisibility() == View.VISIBLE) {
//                        return;
//                    }
//                    loadingLottie.setVisibility(View.VISIBLE);
//                    loadingLottie.setAnimation("T.json");
//                    loadingLottie.setRepeatCount(INFINITE);
//                    loadingLottie.playAnimation();
//                } else {
//                    if (loadingLottie.getVisibility() == View.GONE) {
//                        return;
//                    }
//                    loadingLottie.setVisibility(View.GONE);
//                    loadingLottie.cancelAnimation();
//                }
                break;
        }
        hasProgress(loading, isVisible);
    }

    /**
     * 设置是否显示加载进度
     *
     * @param isVisible 是否显示
     */
    public void hasProgress(RelativeLayout loading, boolean isVisible) {
        if (loading != null) {
            if (isVisible && loading.getVisibility() == View.VISIBLE) {
                return;
            }
            loading.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * 设置是否显示ActionBar
     *
     * @param isVisible 是否显示
     */
    public void hasActionBar(boolean isVisible) {
        actionbar.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置title背景图片
     *
     * @param resId 图片ID
     */
    public void setBackgroundByRes(int resId) {
        actionbar.setBackgroundResource(resId);
    }

    /**
     * 设置title背景颜色
     *
     * @param color 颜色值
     */
    public void setBackgroundByColor(String color) {
        actionbar.setBackgroundColor(Color.parseColor(color));
    }

    /**
     * 设置是否显示返回键
     *
     * @param isVisible 是否显示
     */
    public void hasBack(boolean isVisible) {
        if (isVisible) {
            actionbarBack.setVisibility(View.VISIBLE);
        } else {
            actionbarBack.setVisibility(View.INVISIBLE);
            actionbarTips.setVisibility(View.GONE);
        }
    }

    /**
     * 设置返回键图片
     *
     * @param resId 图片ID
     */
    public void setBackRes(int resId) {
        actionbarBack.setImageResource(resId);
    }

    /**
     * 设置返回键图片
     *
     * @param imagePath 网络图片对象信息
     */
    public void setBackRes(ImagePath imagePath) {
        GlideUtils.getCircleImageToUrl(mActivity, imagePath, actionbarBack, R.mipmap.ic_launcher);
        // Glide.with(mActivity).load("http://img3.imgtn.bdimg.com/it/u=2631545466,886965387&fm=21&gp=0.jpg").transform(new TransformToCircle(mContext)).into(actionbarBack);
    }

    /**
     * 返回键被点击
     */
    public void onBackClick() {
        exit();
    }

    /**
     * 设置是否显示提示标志
     *
     * @param isVisible 是否显示
     */
    public void hasActionBarTips(boolean isVisible) {
        actionbarTips.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置是否显示标题
     *
     * @param isVisible 是否显示
     */
    public void hasTitle(boolean isVisible) {
        actionbarTitle.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置标题
     *
     * @param resId 标题StringID
     */
    public void setTitle(int resId) {
        setTitle(getResources().getString(resId));
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitle(String title) {
        hasActionBar(true);
        actionbarTitle.setText(title);
    }

    /**
     * 设置标题颜色
     *
     * @param color 颜色值
     */
    public void setTitleColor(String color) {
        actionbarTitle.setTextColor(Color.parseColor(color));
    }

    /**
     * 设置标题颜色
     *
     * @param colorRes 颜色值
     */
    public void setTitleColor(int colorRes) {
        actionbarTitle.setTextColor(colorRes);
    }

    /**
     * 设置是否显示分割线
     *
     * @param isVisible 是否显示
     */
    public void hasBottomLine(boolean isVisible) {
        actionbarBottomLine.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置标题颜色
     *
     * @param color 颜色值
     */
    public void setsBottomLineColor(String color) {
        actionbarBottomLine.setBackgroundColor(Color.parseColor(color));
    }

    /**
     * 设置标题颜色
     *
     * @param colorRes 颜色值
     */
    public void setsBottomLineColor(int colorRes) {
        actionbarBottomLine.setBackgroundColor(colorRes);
    }

    /**
     * 设置是否显示菜单
     *
     * @param isVisible 是否显示
     */
    public void hasMenu(boolean isVisible) {
        actionbarMenu.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    /**
     * 添加Title功能
     *
     * @param view 菜单项视图
     */
    public void addMenuItme(View view, LinearLayout.LayoutParams layoutParams) {
        hasMenu(true);
        actionbarMenu.addView(view, layoutParams);
    }

    /**
     * 添加Title功能
     *
     * @param resId 图片ID
     */
    public ImageView addMenuImageItme(int resId, View.OnClickListener mOnClickListener) {
        ImageView mMenuItme = new ImageView(this);
        mMenuItme.setScaleType(ImageView.ScaleType.CENTER);
        mMenuItme.setImageResource(resId);
        mMenuItme.setOnClickListener(mOnClickListener);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) getResources().getDimension(R.dimen.dp_48), LinearLayout.LayoutParams.MATCH_PARENT);
        // params.leftMargin = (int) getResources().getDimension(R.dimen.dp_4);
        addMenuItme(mMenuItme, params);
        return mMenuItme;
    }

    /**
     * 添加Title功能
     *
     * @param textId 文字ID
     */
    public TextView addMenuTextItme(int textId, View.OnClickListener mOnClickListener) {
        return addMenuTextItme(textId, R.color.colorTheme, mOnClickListener);
    }

    /**
     * 添加Title功能
     *
     * @param textId       文字ID
     * @param textColorRes 文字颜色资源id
     */
    public TextView addMenuTextItme(int textId, int textColorRes, View.OnClickListener mOnClickListener) {
        TextView mTextView = new TextView(this);
        mTextView.setTextAppearance(mContext, R.style.ToolBarStyle);
        // mTextView.setMaxWidth((int) getResources().getDimension(R.dimen.dp_144));
        int padding = (int) getResources().getDimension(R.dimen.dp_6);
        mTextView.setPadding(padding, 0, padding, 0);
        mTextView.setGravity(Gravity.CENTER_VERTICAL);
        mTextView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
        mTextView.setTextColor(ContextCompat.getColor(mContext, textColorRes));
        mTextView.setText(textId);
        mTextView.setOnClickListener(mOnClickListener);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
        addMenuItme(mTextView, params);
        return mTextView;
    }

    /**
     * 设置底部线的可见性
     *
     * @param isVisible 是否显示
     */
    public void setBottomLineVisibility(boolean isVisible) {
        actionbarBottomLine.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showMessage(String message) {
        ToastUtils.show(mContext, message);
    }

    @Override
    public void showProgress() {
        if (!this.isFinishing()) {
            hasProgress(true);
        }
    }

    @Override
    public void hideProgress() {
        hasProgress(false);
    }

    @Override
    public boolean useLightMode() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }
}
